package main

import (
	"context"
	"fmt"
	"gorm.io/driver/postgres"
	"mall/internal/conf"
	"mall/internal/model"
	"mall/pkg/env"
	"mall/pkg/orm"
)

func main() {
	if e := conf.Conf.Init(context.Background()); e != nil {
		fmt.Println(e.Error())
		return
	}
	pg := postgres.Open(conf.Conf.ORM.DSN)
	db, err := orm.NewConnection(conf.Conf.ORM, env.Local, pg)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	migrator := db.Migrator()
	tables := []interface{}{
		&model.Schedule{},
	}
	for i := len(tables); i > 0; i-- {
		table := tables[i-1]
		if migrator.HasTable(table) {
			if e := migrator.DropTable(table); e != nil {
				fmt.Println(e.Error())
				return
			}
		}
	}
	for _, table := range tables {
		err = migrator.AutoMigrate(table)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
	}
}
